package com.heima.search.listen;

import com.alibaba.fastjson.JSON;
import com.heima.common.constants.message.ArticleForEsConstants;
import com.heima.feigns.article.ArticleFeign;
import com.heima.model.search.vo.SearchArticleVo;
import com.heima.search.service.ArticleSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

@Component
@Slf4j
public class ArticleAddOrRemoveListener {

    @Resource
    ArticleSearchService articleSearchService;

    @KafkaListener(topics = ArticleForEsConstants.ARTICLE_ELASTICSEARCH_ADD)
    public void addArticle(String message) {
        Map map = JSON.parseObject(message, Map.class);
        String articleId = (String) map.get("articleId");
        log.info("搜索微服务 接收到添加索引库文章消息==> {}",articleId);

        SearchArticleVo article = (SearchArticleVo) map.get("article");
        log.info("搜索微服务 接收到添加索引库文章消息==> {}",article);
        articleSearchService.saveArticle(articleId, article);
    }

    @KafkaListener(topics = ArticleForEsConstants.ARTICLE_ELASTICSEARCH_DELETE)
    public void removeArticle(String message) {
        Map map = JSON.parseObject(message, Map.class);
        String articleId = (String) map.get("articleId");
        log.info("搜索微服务 接收到删除索引库文章消息==> {}",articleId);
        articleSearchService.deleteArticle(articleId);
    }
}
